package com.emergency.dfs.controller;

import com.emergency.dfs.service.DFSService;
import com.emergency.common.entity.Result;
import com.emergency.common.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Created by lijie on 2019/10/17.
 */
@RestController
@RequestMapping("/dfs")
@Api(value="文件存储类",description = "文件存储服务器，提供文件上传下载")
public class DFSController {

    @Autowired
    private DFSService dfsService;

    @ApiOperation(value = "上传接口" ,  notes="文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, paramType = "query", dataType = "MultipartFile")
    })
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "file",required = true) MultipartFile file){
        String filePath=dfsService.uploadFile(file);
        return new Result(true, StatusCode.OK,  "上传成功", filePath);
    }

    @ApiOperation(value = "下载接口" ,  notes="文件下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUrl", value = "文件地址", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "file", value = "文件存储地址", required = true, paramType = "query", dataType = "File")
    })
    @RequestMapping(value = "download",method = RequestMethod.GET)
    public Result download(String fileUrl, File file){
        boolean flag=dfsService.downloadFile(fileUrl,file);
        return new Result(flag, StatusCode.OK,  "下载成功", "");
    }

    @ApiOperation(value = "删除接口" ,  notes="文件删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件地址", required = true, paramType = "update", dataType = "String")
    })
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public Result delete(String fileUrl){
        boolean flag=dfsService.deleteFile(fileUrl);
        return new Result(flag, StatusCode.OK,  "删除成功", "");
    }
}
